package com.my.algorithmlearning.leetcode;

import com.my.algorithmlearning.entity.ListNode;
import org.springframework.util.Assert;

/**
 * @ClassName MergeTwoListRecursive
 * @Description MergeTwoListRecursive
 * @Author wanghaiwei
 * @Date 2022/11/22 9:50
 */
public class MergeTwoListRecursive {

    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        if (list1 == null) {
            return list2;
        }
        if (list2 == null) {
            return list1;
        }
        if (list1.val < list2.val) {
            list1.next = mergeTwoLists(list1.next, list2);
            return list1;
        }else {
            list2.next = mergeTwoLists(list1, list2.next);
            return list2;
        }
    }

    public static void main(String[] args) {
        ListNode list11 = new ListNode(1);
        ListNode list12 = new ListNode(2);
        ListNode list13 = new ListNode(4);
        ListNode list21 = new ListNode(1);
        ListNode list22 = new ListNode(3);
        ListNode list23 = new ListNode(4);
        list11.next = list12;
        list12.next = list13;
        list21.next = list22;
        list22.next = list23;
        MergeTwoListRecursive solution = new MergeTwoListRecursive();
        String expected = "1,1,2,3,4,4";
        ListNode resultLink = solution.mergeTwoLists(list11, list21);
        String resultStr = resultLink.toString();
        Assert.isTrue(expected.equals(resultStr), "结果错误");
    }
}
